/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package loginRegister;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

/**
 *
 * @author 黄忠
 */
public class LoginServlet extends HttpServlet {
    public void wrong(){
        String msg="用户名或密码不正确！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String userName=new String(request.getParameter("userName").getBytes("ISO-8859-1"),"UTF-8");
        String password=new String(request.getParameter("password").getBytes("ISO-8859-1"),"UTF-8");
        LoginBean nn=new LoginBean();
        nn.setUserName(userName);
        nn.setPassword(password);
        if(nn.login()){
            HttpSession session=request.getSession();//获取session对象
            ArrayList login=new ArrayList();//实例化列表对象
            login.add(nn);
            session.setAttribute("login", login);//把列表保存到session对象中，以便在别的页面中获取个人信息
            response.sendRedirect("http://localhost:8084/ShiYan5/main/main.jsp");
        }
        else {
            wrong();
            response.sendRedirect("http://localhost:8084/ShiYan5/login.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doGet(request, response);
    }     
}
